import android.content.Context
import android.media.AudioAttributes
import android.media.SoundPool
import com.sf.innovate.hammer.game.R

class GameAudioManager(context: Context) {
    private val soundPool: SoundPool by lazy {
        val attribs = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_GAME)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build()
        SoundPool.Builder().setMaxStreams(10).setAudioAttributes(attribs).build()
    }

    private val soundMap = HashMap<Int, Int>()

    init {
        // 加载音效资源
        soundMap.put(R.raw.countdown, soundPool.load(context, R.raw.countdown, 1))
        soundMap.put(R.raw.failed, soundPool.load(context, R.raw.failed, 2))
        soundMap.put(R.raw.rush, soundPool.load(context, R.raw.rush, 3))
        soundMap.put(R.raw.success, soundPool.load(context, R.raw.success, 4))
    }

    fun playSound(sound: Int) {
        val soundId = soundMap[sound] ?: return
        soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f)
    }
}